#include<vector>
#include<string>
#include<stack>
using namespace std;

class Solution {
public:
    string removeKdigits(string num, int k) {
        if (num.size() <= k)return "0";
        stack<int>stack_;
        
        vector<int>stack;
        for (int i = 0; i < num.size(); ++i) {
            while (stack.size() > 0 && k > 0 && num[stack.back()] > num[i]) {
                stack.pop_back();
                --k;
            }
            stack.push_back(i);
        }
        while (k > 0) {
            stack.pop_back();
            --k;
        }
        string res="";
        bool flag = true;
        for (int i = 0; i < stack.size(); ++i) {
            if (flag&& num[stack[i]] == '0')
                continue;
            flag = false;
            res += num[stack[i]];
        }
        if (res == "")
            res = "0";
        return res;
    }
};